package zisu.netty.practice.RPC.config;

import lombok.extern.slf4j.Slf4j;
import zisu.netty.practice.RPC.config.provider.ProviderConfig;
import zisu.netty.practice.RPC.server.RpcServer;

import java.util.List;

/**
 * @Desc: "服务器端启动配置类。"
 * @Author: caixiang
 * @DATE: 2021/1/27 13:49
 */
//启动bean 和spring 有一个比较好的整合。
@Slf4j
public class RpcServerConfig {
    private final String host = "127.0.0.1";
    protected int port;
    private List<ProviderConfig> providerConfigList;
    private RpcServer rpcServer = null;

    public RpcServerConfig(List<ProviderConfig> providerConfigList){
        this.providerConfigList = providerConfigList;
    }

    public void exporter(){
        if(rpcServer == null){
            try {
                rpcServer = new RpcServer(host + ":" + port);
            }catch (Exception e){
                log.error("RpcServerConfig exporter exception:"+e);
            }
        }
        for(ProviderConfig providerConfig: providerConfigList){
            //把config 注册到 rpcServer 上。
            rpcServer.registerProcessor(providerConfig);
        }

    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public List<ProviderConfig> getProviderConfigList() {
        return providerConfigList;
    }

    public void setProviderConfigList(List<ProviderConfig> providerConfigList) {
        this.providerConfigList = providerConfigList;
    }
}
